/**
 * zhanjun
 */

$(function() {
	
	/*
	 * 获取传入的参数
	 */
	var idres=getUrlParamValue("idResource");
	

	
	$('#treebox').tree({
	    url:'../resource/ser_getResTree.do?idResource='+idres,
	    
	    onDblClick:function(node){
	    	var data = jQuery.parseJSON(node.attributes);
	    	$("#resName").attr("value",data.resName);
	    	$("#resource").attr("value",data.resource);
	    	$("#resDesc").attr("value",data.resDesc);
	    	$("#idResource").attr("value",data.idResource);
	    	$("#level").attr("value",data.level);
	    	$("#orderby").attr("value",data.orderby);
	    	$("#parentId").attr("value",data.parentId);
	    	$("#resType").attr("value",data.resType);
	    	$("#systemId").attr("value",data.systemId); 	
	    }
	});
	$("#delTreeItem").click(function() {
		//删除
		var selectedNode=$('#treebox').tree('getSelected');	
			//alert(id);
			if(selectedNode==null)
			{
				alert("请选择树中的节点。");					
			}
			else{
				if(selectedNode.id=="-99")
				{//没有在数据库中保存
					$('#treebox').tree('remove',selectedNode.target);					
					return true;
				}
				else(confirm("是否确定删除此选中的节点？"))
				{						
					var postdata = "";							
					postdata = "ids=" + selectedNode.id;				
					$.ajax({
						type : "POST",
						url : "../resource/ser_delete.do",
						data : postdata,
						success : function(msg) {						
							alert(msg);
							proccessSession(msg);//处理session 过期
							$('#datatable').datagrid("reload");
						}
					});
					$('#treebox').tree('remove',selectedNode.target);
				}
			}		
		
		
	});
	//获取树的层次
var	Get_TreeLevel=function(node){
	var level=0;	
	while($('#treebox').tree('getParent',node.target)!=null){
		node=$('#treebox').tree('getParent',node.target);
		level++;
	}
	return level;
}
//获取节点的排序
var Get_NodeOrder=function(node){
	 
	var pnode=$('#treebox').tree('getParent',node.target);
	 if(pnode!=null){		 
		var childrenNodes=$('#treebox').tree('getChildren',pnode.target);
		for(var i=0;i<childrenNodes.length;i++){
			if(childrenNodes[i].id==node.id){return i;}
			
		}
		 
	 }
	 
      return -1;
}
$("#newTreeItem").click(function() {
	var newNode= $('#treebox').tree('find',-99);
	if(newNode!=null){
		alert("请先保存新节点的数据！");
		return false;
	}
	//新建节点
	$("#resName").attr("value","新节点");
	$("#resource").attr("value","http://");
	$("#resDesc").attr("value","写点什么描述吧");
	$("#idResource").attr("value","");
	
	var selectedNode=$('#treebox').tree('getSelected');	
	if(!selectedNode){
		selectedNode=$('#treebox').tree('getRoot');
	}
	var nodes = [{
        "id":-99,
        "text":"新节点"
    }];
    $('#treebox').tree('append', {
        parent:selectedNode.target,
        data:nodes
    });
    var newNode= $('#treebox').tree('find',-99);  
    $('#treebox').tree('select',newNode.target);    
	var level =Get_TreeLevel(newNode);
	var parentId =selectedNode.id;
	var orderby=Get_NodeOrder(newNode);
	
	$("#level").attr("value",level);
	$("#orderby").attr("value",orderby);
	$("#parentId").attr("value",parentId);
	$("#resType").attr("value","0");
	$("#systemId").attr("value",idres); 	 
	    
	});
	
	$("#btn_reset").click(function() {
		document.getElementById("dataform").reset();

		location.href='resource-admin.html';
		
	});
	$("#returnMag").click(function() {
		location.href='resource-admin.html';
		
	});
	
	/*
	 * 刷新页面
	 */

	$("#reflash").click(function() {
		location.reload();
		
	});
	/*
	 * 保存事件
	 */

	$("#btn_submit").click(function() {
		var postdata = $('#dataform').serialize();
		
		if($('#dataform').form('validate')){		
			$.ajax({
				type : "POST",
				url : "../resource/ser_saveItem.do",
				data : postdata,
				success : function(msg) {
					alert(msg);
					 var newNode= $('#treebox').tree('find',-99);					 
					$('#treebox').tree('reload',newNode.target);
					document.getElementById("dataform").reset();					
				}
			});
		}
	});

});
